﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Navigation;
using Orchard.Widgets;
using Orchard.Widgets.Services;

namespace NGM.Portlets.Providers {
    public class WidgetTypeMenuNavigationProvider : IPortletNavigationProvider {
        private readonly IWidgetsService _widgetsService;

        public WidgetTypeMenuNavigationProvider(IWidgetsService widgetsService) {
            _widgetsService = widgetsService;

            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public string MenuName {
            get { return "WidgetTypeMenu"; }
        }

        public void GetNavigation(NavigationBuilder builder) {
            builder.AddImageSet("widgets")
                .Add(T("Widgets"), "5",
                    AttachWidgetTypes);
        }

        private void AttachWidgetTypes(NavigationItemBuilder menu) {
            var widgetTypes = _widgetsService.GetWidgetTypes().ToList();

            if (!widgetTypes.Any())
                return;

            foreach (var widgetType in widgetTypes.OrderBy(type => (string)type.Item1)) {
                string name = widgetType.Item1;
                string description = widgetType.Item2;

                menu.Add(T(name), "0", item => item.Action("Index", "Admin", new { area = "Orchard.Widgets" }));
            }
        }
    }
}